<?php
namespace Admin\Controller;
use Common\Controller\AdminController;
/*
 * 参赛申请
 * */
class CssqController extends AdminController{
	//动作名称
	private $_act_show = array(
		"0"	=> "参赛报名",
		"1"	=> "系部审核",
		"2"	=> "学校审核",
	);
	//0：环节进行中，1：当前环节结束
	private $_act_status = array(
		"0"	=> "进行中",
		"1"	=> "结束",
	);
	protected function _initialize(){
		parent::_initialize();
		$this->assign('_act_show', $this->_act_show);
		$this->assign('_act_status', $this->_act_status);
	}
	//赛事信息
	public function index(){
		$where = 'pid = 3';
		$where .= I('get.pid', -1) > -1 ? ' and n.pid=' . I('get.pid', 0, 'intval') :'';
		$where .= I('get.title') ? ' and n.title like "%' . I('get.title') . '%"' : '';
		$where .= I('get.status_query', -1) > -1 ? ' and n.status=' . I('get.status_query') : '';
		$where .= I('get.start_time') ? ' and n.times>=' . strtotime(I('get.start_time')) : '';
		$where .= I('get.end_time') ? ' and n.times<' . (strtotime(I('get.end_time')) + 24*3500) : '';

		$list = $this->getList('n.id,n.title,n.times,n.view,n.js_cslb,n.js_csxm,j.num', 'news n', $where,
			'sort desc,id desc', true,12,
			"LEFT JOIN (select news_id,count(news_id) num from ".C('DB_PREFIX')."js_bmsq group by news_id) j on j.news_id = n.id"
			);
		$this->assign('list', $list);
		$this->display();
	}
	//竞赛项目的报名
	public function sq_list(){
		$where = "news_id = ".I("get.id",0,"intval");

		$this->_ssxx();

	   	$list = $this->getList('*', 'js_bmsq', $where, 'id desc', true);
		$this->assign('list', $list);
	    $this->display();
	}

	/**
	 * 赛事信息浏览
	 */
	public function select(){
		$info = $this->getInfo('*', 'js_bmsq', 'id=' . I('get.id', 0, 'intval'));
		$this->assign('info', $info);
		$list_yj = $this->getList('*', 'public_action', 'table_id=' . I('get.id', 0, 'intval'));
		$this->assign('list_yj', $list_yj);
		$this->display();
	}
	//赛事信息
	private function _ssxx($_id = null){
		if(is_null($_id)){
			$_id =I('get.id', 0, 'intval');
		}
		$ssxx = $this->getInfo('*', 'news', 'id=' . $_id);
		$this->assign('ssxx', $ssxx);
	}
	/*参赛报名*/
	public function add(){
		$_baseInfo = new \Admin\Model\JsBaseModel();
		if(IS_POST){
			if(!$_POST['pic']){
				$this->returnResult(false,"报名必须上传照片");
			}
			$_POST['cslb_name'] = $_baseInfo->getNameById(I("post.cslb_id",0));
			$_POST['csxm_name'] = $_baseInfo->getNameById(I("post.csxm_id",0));
			$result = $this->updateData($_POST, 'js_bmsq');
			$this->returnResult($result);
		}else{
			$this->_ssxx();
			$this->display();
		}
	}
	/*报名修改*/
	public function edit(){
		$_baseInfo = new \Admin\Model\JsBaseModel();
		if(IS_POST){
			if(!$_POST['pic']){
				$this->returnResult(false,"报名必须上传照片");
			}
			$_POST['cslb_name'] = $_baseInfo->getNameById(I("post.cslb_id",0));
			$_POST['csxm_name'] = $_baseInfo->getNameById(I("post.csxm_id",0));
			$result = $this->updateData($_POST, 'js_bmsq', 2);
			$this->returnResult($result);
		}else{
			$info = $this->getInfo('*', 'js_bmsq', array('id' => I('get.id', 0, 'intval')));
			$this->_ssxx($info['news_id']);
			$this->assign('info', $info);
			$this->display('add');
		}
	}
	/*删除报名*/
	public function del(){
		$result = $this->deleteData('id=' . I('get.id', 0, 'intval'), 'js_bmsq');
		$this->returnResult($result);
	}
	//审核自动，加载的字段
	private function _autoSetActInfo($_opt){
		$info_act = array(
			"dep_id"		=>	"",
			"dep_name"		=>	"",
			"uid"			=>	session('adminInfo.id'),
			"user_name"		=>	session('adminInfo.name'),
			"act_times"		=>	time(),
			"table_name"	=>	"js_lxsb",
		);
		return array_merge($_opt,$info_act);
	}
	/*系部管理*/
	public function xbgl(){
		$where = "act_id > 0 ";
		$list = $this->getList('*', 'js_bmsq', $where, 'id desc', true);
		$this->assign('list', $list);
		$this->display();
	}
	/*参赛申请审核*/
	public function xbgl_set(){
		if(IS_POST){
			$_id 		= I('post.id', 0, 'intval');
			$_table_id 	= I('post.table_id', 0, 'intval');
			$_set_opt =	array(
				"id"			=>	$_id,
				"table_id"		=>	$_table_id,
				"act_content"	=>	I('post.act_content'),
				"act_remake"	=>	I('post.act_remake'),
				"status"		=>	I('post.act_type') == "saveSubmit"?1:0	//0：环节进行中，1：当前环节结束
			);
			$info_act = $this->_autoSetActInfo($_set_opt);
			$result = $this->updateData($info_act, 'public_action',2);

			if($result && I('post.act_type') == "saveSubmit"){
				//更新立项申报表的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"2",	//环节2
					"status" 		=>	"0",	//环节当前状态：待审核
					"act_times"		=>	time(),
				);
				$this->updateData($_save, 'js_bmsq', 2);
			}
			$this->returnResult($result,null,U("Cssq/xbgl"));
		}else{
			$_table_id = I('get.id', 0, 'intval');
			$info = $this->getInfo('*', 'js_bmsq', 'id=' . $_table_id);
			$this->assign('info', $info);

			$this->_ssxx($info['news_id']);

			if($info['status'] == '0'){
				//更新立项申报表的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"1",	//环节1
					"status" 		=>	"0",	//环节当前状态：正在审核
					"act_times"		=>	time(),
				);
				$this->updateData($_save, 'js_bmsq', 2);
			}

			//自动产生默认审核信息
			$info_act = $this->getInfo('*', 'public_action', "table_name = 'js_bmsq' and act_id = 1 and table_id = ". $_table_id);
			if(is_null($info_act)){
				$_opt = array(
					"act_id"		=>	"1",
					"table_id"		=>	$_table_id
				);
				$info_act = $this->_autoSetActInfo($_opt);
				$result = $this->updateData($info_act, 'public_action');
				$info_act['id'] = $result;
			}
			$this->assign('info_act', $info_act);
			$this->display();
		}
	}
	/**
	 * 删除信息
	 */
	public function delxbgl(){
		$result = $this->deleteData(array('id' => I('get.id', 0, 'intval')), 'js_bmsq');
		$this->returnResult($result);
	}
	/*学校管理*/
	public function xxgl(){
		$where = "act_id > 1";
		$list = $this->getList('*', 'js_bmsq', $where, 'id desc', true);
		$this->assign('list', $list);
		$this->display();
	}

	/*学校审核*/
	public function xxgl_set(){
		if(IS_POST){
			$_id 		= I('post.id', 0, 'intval');
			$_table_id 	= I('post.table_id', 0, 'intval');
			$_set_opt =	array(
				"id"			=>	$_id,
				"table_id"		=>	$_table_id,
				"act_content"	=>	I('post.act_content'),
				"act_remake"	=>	I('post.act_remake'),
				"status"		=>	I('post.act_type') == "saveSubmit"?1:0	//0：环节进行中，1：当前环节结束
			);
			$info_act = $this->_autoSetActInfo($_set_opt);
			$result = $this->updateData($info_act, 'public_action',2);

			if($result && I('post.act_type') == "saveSubmit"){
				//更新立项申报表的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"2",	//环节2
					"status" 		=>	"1",	//环节当前状态：审核结束
					"act_times"		=>	time(),
				);
				$this->updateData($_save, 'js_bmsq', 2);
			}
			$this->returnResult($result,null,U("Cssq/xxgl"));
		}else{
			$_table_id = I('get.id', 0, 'intval');
			$info = $this->getInfo('*', 'js_bmsq', 'id=' . $_table_id);
			$this->assign('info', $info);

			if($info['status'] == '0'){
				//更新立项申报表的环节信息
				$_save =	array(
					"id"			=>	$_table_id,
					"act_id"		=>	"2",	//环节2
					"status" 		=>	"0",	//环节当前状态：正在审核
					"act_times"		=>	time(),
				);
				$this->updateData($_save, 'js_bmsq', 2);
			}

			//自动产生默认审核信息
			$info_act = $this->getInfo('*', 'public_action', "table_name = 'js_bmsq' and act_id = 2 and table_id = ". $_table_id);
			if(is_null($info_act)){
				$_opt = array(
					"act_id"		=>	"2",
					"table_id"		=>	$_table_id
				);
				$info_act = $this->_autoSetActInfo($_opt);
				$result = $this->updateData($info_act, 'public_action');
				$info_act['id'] = $result;
			}
			$this->assign('info_act', $info_act);
			$this->display();
		}
	}

}